﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using FineUIPro;

namespace CanDoo.Oil.WebApp.proProduct
{
    public partial class Edit : EditPagebase<IProductContract, Product>
    {
        public override bool IsMaxMode
        {
            get
            {
                return true;
            }
        }
        public IProductTypeContract ProductTypeContract { get; set; }
        public IBrandContract BrandContract { get; set; }
        public ISupplierContract SupplierContract { get; set; }
        public IProductUnitContract ProductUnitContract { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FineUIBindList.BindWhetherOrNot(ed_FocusTracking, true);
                FineUIBindList.BindWhetherOrNot(ed_IsNew, true);
                FineUIBindList.BindWhetherOrNot(ed_EasyStock, true);
                //FineUIBindList.BindWhetherOrNot(ed_IsOil, true);
                //FineUIBindList.BindWhetherOrNot(ed_IsOilSales, true);
                //FineUIBindList.BindWhetherOrNot(ed_ExchangeIntegral, true);
                //FineUIBindList.BindWhetherOrNot(ed_FrontDesk, true);
                //FineUIBindList.BindWhetherOrNot(ed_NoIntegral, true);
                //FineUIBindList.BindWhetherOrNot(ed_NoDiscount, true);
                FineUIBindList.BindWhetherOrNot(ed_Enable, true);
                FineUIBindList.BindWhetherOrNot(ed_ManageSerialNum, true);
                //FineUIBindList.BindWhetherOrNot(ed_Unit, true);
                

                //FineUIListHelper.BindDDL<ProductType>(ProductTypeContract.GetResolvedTree(), 0L, ed_ProductType,true);
                //FineUIListHelper.BindDDL(ProductTypeContract.EntityName, this.ProductTypeContract.GetSelectList(), 0L, ed_ProductType,true);
                //FineUIListHelper.BindDDL<Brand>(BrandContract.GetResolvedTree(), 0L, ed_Brand,true);
                //FineUIListHelper.BindDDL(BrandContract.EntityName, this.BrandContract.GetSelectList(), 0L, ed_Brand,true);
                //FineUIListHelper.BindDDL<Supplier>(SupplierContract.GetResolvedTree(), 0L, ed_Supplier);
                //FineUIListHelper.BindDDL(SupplierContract.EntityName, this.SupplierContract.GetSelectList(), 0L, ed_Supplier);
                //FineUIListHelper.BindDDL<ProductUnit>(ProductUnitContract.GetResolvedTree(), 0L, ed_ProductUnit,true);
                FineUIListHelper.BindDDL(ProductUnitContract.EntityName, this.ProductUnitContract.GetSelectList(), 0L, ed_ProductUnit,true);
                if (PageMode != pagemode.Add)
                    LoadData();
            }
        }

        protected override void LoadData(Product entity)
        {
            ed_Name.SetValue(entity.Name);
            ed_SN.SetValue(entity.SN);
            ed_Code.SetValue(entity.Code);
            ed_ShortName.SetValue(entity.ShortName);
            if (entity.ProductType!=null)
            {
                ed_ProductType.Values=new string[] { entity.ProductType.Id.ToString() };
                ed_ProductType.Text=entity.ProductType.Name;
            }
            if (entity.Brand!=null)
            {
                ed_Brand.Values=new string[] { entity.Brand.Id.ToString() };
                ed_Brand.Text=entity.Brand.Name;
            }
            if (entity.DefaultSupplier!=null)
            {
                ed_DefaultSupplier.Values=new string[] { entity.DefaultSupplier.Id.ToString() };
                ed_DefaultSupplier.Text=entity.DefaultSupplier.Name;
            }
            if (!entity.ProductUnitId.IsNull())
                ed_ProductUnit.SetValue(entity.ProductUnitId);
            ed_Specifications.SetValue(entity.Specifications);
            ed_Model.SetValue(entity.Model);
            ed_Weight.SetValue(entity.Weight);
            ed_Unit.SetValue(entity.Unit);
            //ed_ImgUrl1.SetValue(entity.ImgUrl1);
            //ed_ImgUrl2.SetValue(entity.ImgUrl2);
            //ed_ImgUrl3.SetValue(entity.ImgUrl3);
            //ed_ImgUrl4.SetValue(entity.ImgUrl4);
            //ed_ImgUrl5.SetValue(entity.ImgUrl5);
            ed_Year.SetValue(entity.Year);
            ed_MarketTime.SetValue(entity.MarketTime);
            ed_Outline.SetValue(entity.Outline);
            ed_Explain.SetValue(entity.Explain);
            ed_TechnicalParameter.SetValue(entity.TechnicalParameter);
            ed_CommonProblem.SetValue(entity.CommonProblem);
            ed_Remark.SetValue(entity.Remark);
            ed_MarkedPrice.SetValue(entity.MarkedPrice);
            ed_DefaultCostPrice.SetValue(entity.DefaultCostPrice);
            ed_RetailPrice.SetValue(entity.RetailPrice);
            //ed_VIPPrice1.SetValue(entity.VIPPrice1);
            //ed_VIPPrice2.SetValue(entity.VIPPrice2);
            //ed_VIPPrice3.SetValue(entity.VIPPrice3);
            //ed_VIPPrice4.SetValue(entity.VIPPrice4);
            //ed_TradePrice1.SetValue(entity.TradePrice1);
            //ed_TradePrice2.SetValue(entity.TradePrice2);
            //ed_TradePrice3.SetValue(entity.TradePrice3);
            //ed_TradePrice4.SetValue(entity.TradePrice4);
            //ed_TradePrice5.SetValue(entity.TradePrice5);
            //ed_TradePrice6.SetValue(entity.TradePrice6);
            //ed_TradePrice7.SetValue(entity.TradePrice7);
            //ed_TradePrice8.SetValue(entity.TradePrice8);
            ed_FocusTracking.SetValue(entity.FocusTracking);
            ed_IsNew.SetValue(entity.IsNew);
            ed_EasyStock.SetValue(entity.EasyStock);
            //ed_IsOil.SetValue(entity.IsOil);
            //ed_IsOilSales.SetValue(entity.IsOilSales);
            //ed_ExchangeIntegral.SetValue(entity.ExchangeIntegral);
            //ed_FrontDesk.SetValue(entity.FrontDesk);
            //ed_NoIntegral.SetValue(entity.NoIntegral);
            //ed_NoDiscount.SetValue(entity.NoDiscount);
            ed_Enable.SetValue(entity.Enable);
            ed_ManageSerialNum.SetValue(entity.ManageSerialNum);
        }

        protected override OperationResult SaveData(Product entity)
        {
            entity.Name = ed_Name.GetValue().ToString();
            entity.SN = ed_SN.GetValue().ToString();
            entity.Code = ed_Code.GetValue().ToString();
            entity.ShortName = ed_ShortName.GetValue().ToString();
            if (ed_ProductType.Values.Count() > 0 && ed_ProductType.Values[0].ToLong() >  0L)
                entity.ProductTypeId = ed_ProductType.Values[0].ToLong();
            else
                entity.ProductTypeId = 0L;
            if (ed_Brand.Values.Count() > 0 && ed_Brand.Values[0].ToLong() >  0L)
                entity.BrandId = ed_Brand.Values[0].ToLong();
            else
                entity.BrandId = 0L;
            if (ed_DefaultSupplier.Values.Count() > 0 && ed_DefaultSupplier.Values[0].ToLong() >  0L)
                entity.DefaultSupplierId = ed_DefaultSupplier.Values[0].ToLong();
            else
                entity.DefaultSupplierId = null;
            if (ed_ProductUnit.GetValue().ToLong() == 0L)
                entity.ProductUnitId = 0L;
            else
                entity.ProductUnitId = ed_ProductUnit.GetValue().ToLong();
            entity.Specifications = ed_Specifications.GetValue().ToString();
            entity.Model = ed_Model.GetValue().ToString();
            entity.Weight = ed_Weight.GetValue().ToDouble();
            entity.Unit = ed_Unit.GetValue().ToString();
            //entity.ImgUrl1 = ed_ImgUrl1.GetValue().ToString();
            //entity.ImgUrl2 = ed_ImgUrl2.GetValue().ToString();
            //entity.ImgUrl3 = ed_ImgUrl3.GetValue().ToString();
            //entity.ImgUrl4 = ed_ImgUrl4.GetValue().ToString();
            //entity.ImgUrl5 = ed_ImgUrl5.GetValue().ToString();
            entity.Year = ed_Year.GetValue().ToString();
            entity.MarketTime = ed_MarketTime.GetValue().ToNullableDateTime();
            if(entity.MarketTime ==DateTime.MinValue)
                entity.MarketTime = null;
            entity.Outline = ed_Outline.GetValue().ToString();
            entity.Explain = ed_Explain.GetValue().ToString();
            entity.TechnicalParameter = ed_TechnicalParameter.GetValue().ToString();
            entity.CommonProblem = ed_CommonProblem.GetValue().ToString();
            entity.Remark = ed_Remark.GetValue().ToString();
            entity.MarkedPrice = ed_MarkedPrice.GetValue().ToFloat();
            entity.DefaultCostPrice = ed_DefaultCostPrice.GetValue().ToFloat();
            entity.RetailPrice = ed_RetailPrice.GetValue().ToFloat();


            //entity.VIPPrice1 = ed_VIPPrice1.GetValue().ToFloat();
            //entity.VIPPrice2 = ed_VIPPrice2.GetValue().ToFloat();
            //entity.VIPPrice3 = ed_VIPPrice3.GetValue().ToFloat();
            //entity.VIPPrice4 = ed_VIPPrice4.GetValue().ToFloat();
            //entity.TradePrice1 = ed_TradePrice1.GetValue().ToFloat();
            //entity.TradePrice2 = ed_TradePrice2.GetValue().ToFloat();
            //entity.TradePrice3 = ed_TradePrice3.GetValue().ToFloat();
            //entity.TradePrice4 = ed_TradePrice4.GetValue().ToFloat();
            //entity.TradePrice5 = ed_TradePrice5.GetValue().ToFloat();
            //entity.TradePrice6 = ed_TradePrice6.GetValue().ToFloat();
            //entity.TradePrice7 = ed_TradePrice7.GetValue().ToFloat();
            //entity.TradePrice8 = ed_TradePrice8.GetValue().ToFloat();
            entity.VIPPrice1 = 0;
            entity.VIPPrice2 = 0;
            entity.VIPPrice3 = 0;
            entity.VIPPrice4 = 0;
            entity.TradePrice1 = 0;
            entity.TradePrice2 = 0;
            entity.TradePrice3 = 0;
            entity.TradePrice4 = 0;
            entity.TradePrice5 = 0;
            entity.TradePrice6 = 0;
            entity.TradePrice7 = 0;
            entity.TradePrice8 = 0;


            entity.FocusTracking = ed_FocusTracking.GetValue().ToBool();
            entity.IsNew = ed_IsNew.GetValue().ToBool();
            entity.EasyStock = ed_EasyStock.GetValue().ToBool();

            //entity.IsOil = ed_IsOil.GetValue().ToBool();
            //entity.IsOilSales = ed_IsOilSales.GetValue().ToBool();
            //entity.ExchangeIntegral = ed_ExchangeIntegral.GetValue().ToBool();
            //entity.FrontDesk = ed_FrontDesk.GetValue().ToBool();
            //entity.NoIntegral = ed_NoIntegral.GetValue().ToBool();
            //entity.NoDiscount = ed_NoDiscount.GetValue().ToBool();
            entity.ProductClass = 0;
            entity.ExchangeIntegral = false;
            entity.FrontDesk = false;
            entity.NoIntegral = false;
            entity.NoDiscount = false;

            entity.Enable = ed_Enable.GetValue().ToBool();
            entity.ManageSerialNum = ed_ManageSerialNum.GetValue().ToBool();
            return base.SaveData(entity);
            
        }
    }
}
